/**   
 * @Title: CommonAdapter.java 
 * @author 7828wc   
 * @date 2015年7月29日 下午1:50:02 
 */
package com.onstar.cn.framework.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.onstar.cn.framework.utils.ViewInjectUtils;

import java.util.List;

/**
 * @ClassName: CommonAdapter
 * @Description: Adadpter 适配器基类，主要负责减少Adapter代码量
 * @author 7828wc
 * @date 2015年7月29日 下午1:50:02
 */
public abstract class CommonAdapter<T> extends BaseAdapter {
	protected List<T> datas;
	protected Context context;

	public CommonAdapter(List<T> datas, Context context) {
		this.datas = datas;
		this.context = context;
	}

	/**
	 * (non-Javadoc)
	 * <p>
	 * Title: getCount
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @return
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		if(datas != null) {
			return datas.size();
		}
		return 0;
	}

	/**
	 * (non-Javadoc)
	 * <p>
	 * Title: getItem
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param position
	 * @return
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Object getItem(int position) {
		if(datas !=null) {
			return datas.get(position);
		}
		return null;
	}

	/**
	 * (non-Javadoc)
	 * <p>
	 * Title: getItemId
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param position
	 * @return
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int position) {
		return position;
	}

	/**
	 * (non-Javadoc)
	 * <p>
	 * Title: getView
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param position
	 * @param convertView
	 * @param parent
	 * @return
	 * @see android.widget.Adapter#getView(int, android.view.View,
	 *      android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		CommonViewHolder viewHolder = null;
		boolean isResult = false;
		if (convertView == null) {
			viewHolder = getViewHolder();
			convertView = LayoutInflater.from(context).inflate(layoutId(), null);
			isResult = ViewInjectUtils.injectViews(viewHolder.getClass(), viewHolder, convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (CommonViewHolder) convertView.getTag();
			isResult = true;
		}
		if (isResult)
			getView(position, viewHolder);
		return convertView;
	}

	public abstract void getView(int position, CommonViewHolder viewHolder);

	public abstract int layoutId();

	public abstract CommonViewHolder getViewHolder();

}
